package com.atguigu.springcloud.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @author : wenchao.long
 * @date : Created in 2020/12/19 13:22 周六
 * @description :
 */
@Configuration
public class GateWayConfig {

    /**
     * 配置了一个路由规则
     * 当访问地址http://localhost:9527/guonei时会自动转发到地址：http://news.baidu.com/guonei
     *
     * @param builder RouteLocatorBuilder
     * @return RouteLocator
     */
    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("path_route_guoji"
                        , r -> r.path("/guoji")
                                .uri("http://news.baidu.com"))
                .route("path_route_guonei"
                        , r -> r.before(LocalDateTime.now().minusHours(1).atZone(ZoneId.systemDefault()))
                                .and()
                                .path("/guonei")
                                .uri("http://news.baidu.com"))
                .route("payment_routh"
                        , r -> r.after(LocalDateTime.now().plusMinutes(1).atZone(ZoneId.systemDefault()))
//                                .and()
//                                .cookie("username", "zh.p")
//                                .and()
//                                .header("X-Request-Id", "\\d+")
//                                .and()
//                                .method(HttpMethod.GET, HttpMethod.POST)
                                .and()
                                .path("/payment/**")
                                //.filters(c -> c.addRequestParameter("X-Request-Id", "1024"))
                                .uri("lb://CLOUD-PAYMENT-SERVICE"))
                .build();
    }

}
